const addon = require("./MiniProgramProxy.node");

process.on("message", (params) => {
    if (params.commandId === "getmsg") {
        const res = addon?.GetMsg();
        if (res) {
            process.send(JSON.stringify(res));
        } else {
            process.send(JSON.stringify(JSON.stringify({ content: { commandId: "RestartGetMsg" } })));
        }
    } else if (params.commandId === "RegisterReciver") {
        addon?.RegisterReciver(params.detail);
    } else if (params.commandId === "startSyncDisk") {
        const res = addon?.StartSyncDisk();
        process.send(
            JSON.stringify(JSON.stringify({ content: { commandId: "startSyncDisk", detail: params.detail } }))
        );
    } else if (params.commandId === "existLowVersion") {
        const res = addon?.ExistLowVersion();
        process.send(
            JSON.stringify(
                JSON.stringify({ content: { commandId: "existLowVersion", detail: { params: params.detail, res } } })
            )
        );
    } else if (params.commandId === "queryState") {
        const res = addon?.QueryState();
        process.send(
            JSON.stringify(
                JSON.stringify({ content: { commandId: "queryState", detail: { params: params.detail, res } } })
            )
        );
    } else if (params.commandId === "queryStateBeforeExit") {
        const res = addon?.QueryState();
        process.send(
            JSON.stringify(
                JSON.stringify({
                    content: { commandId: "queryStateBeforeExit", detail: { params: params.detail, res } },
                })
            )
        );
    } else if (params.commandId === "relaunch") {
        !addon?.ExistLowVersion() &&
            addon.SendMessage(
                JSON.stringify({
                    commandId: "quit",
                    detail: {},
                })
            );
        process.send(JSON.stringify(JSON.stringify({ content: { commandId: "relaunch", detail: {} } })));
    } else if (params.commandId === "IsUnCachedFile") {
        const res = addon.IsUnCachedFile(params.detail);
        process.send(
            JSON.stringify(JSON.stringify({ content: { commandId: "IsUnCachedFile", detail: { params: res } } }))
        );
    } else if (params.commandId === "quit") {
        !addon?.ExistLowVersion() &&
            addon.SendMessage(
                JSON.stringify({
                    commandId: "quit",
                    detail: {},
                })
            );
        process.send(JSON.stringify(JSON.stringify({ content: { commandId: "quit", detail: {} } })));
    } else if (params.commandId === "tokenUpdate") {
        addon.SendMessage(JSON.stringify(params));
        setTimeout(() => {
            const res = addon?.QueryState();
            if (res === 5) {
                process.send(JSON.stringify(JSON.stringify({ content: { commandId: "syncdiskOffline", detail: {} } })));
            }
        }, 60000);
    } else {
        addon.SendMessage(JSON.stringify(params));
    }
});
